package servlets;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Usuario;

/**
 * Servlet implementation class BuscaServlet
 */
@WebServlet("/BuscaServl et")
public class BuscaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BuscaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		buscar(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		buscar(request,response);
	}
	
	@SuppressWarnings("unchecked")
	public void buscar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		String nome = request.getParameter("nome");
		String nomeCitacoes = request.getParameter("nomeCitacoes");
		String email = request.getParameter("email");
		
		
		ArrayList<Usuario> listaDeUsuarios = null;
		ArrayList<Usuario> resultSet = new ArrayList<Usuario>();		
		
		String path = getServletContext().getRealPath("/bancodados/login.obj");		
		
		try {
			
			FileInputStream fis = new FileInputStream(path);
			ObjectInputStream ois = new ObjectInputStream(fis);
			listaDeUsuarios = (ArrayList<Usuario>) ois.readObject();
			ois.close();
			for (Usuario u : listaDeUsuarios) {
				if(email != null && !email.equals("") && u.getEmail() != null) {
					if(u.getEmail().contains(email)) {
						resultSet.add(u);
						continue;
					}
				}
				if(nome != null && !nome.equals("") && u.getNome() != null) {
					if(u.getNome().contains(nome)) {
						resultSet.add(u);
						continue;
					}
				}
				
				if(nomeCitacoes != null && !nomeCitacoes.equals("") && u.getNomeCitacoes() != null) {
					if(u.getNomeCitacoes().contains(nomeCitacoes)) {
						resultSet.add(u);
						continue;
					}
				}
			}
		} catch (Exception e) {
		}
		
		String url;			
		
		if(resultSet.size() != 0) {
			request.getSession().setAttribute("resultSet", resultSet);
			url = "./buscaResultado.jsp";
		} else {
			url = "./semResultados.jsp";
		}	
		
		response.setContentType("text/html;charset=UTF-8");  
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(url);
		dispatcher.forward(request, response);
	}	

}
